/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * bocciawii
 * Copyright (C) Grupo de trabajo ULE 2010 <ulewii@googlegroups.com>
 * 
 * teclado is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * teclado is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "menu-layout.h"

MenuLayout::MenuLayout(std::string id, int filas, int cols):
			ScreenLayout(filas, cols)
{
//	_numFilas = filas;
//	_numCols = cols;
	_transparency = .7;
	supermenu = NULL;
	submenus[0] = NULL;
	submenus[1] = NULL;
	submenus[2] = NULL;
	submenus[3] = NULL;
	submenus[4] = NULL;
	_numSubmenus = 0;
	_id = id;	for (int i = 0; i < 10; i++)
		for (int j = 0; j < 10; j++)
	{
			conts[i][j] = new Boton("dummy", 0, 0);
		conts[i][j] = NULL;
	}
}

MenuLayout::~MenuLayout ()
{
	for (int i = 0; i < _numSubmenus; i++)
		submenus[i]->~MenuLayout();
}

void MenuLayout::EstablecerEnlaces()
{
	for (int i = 0; i < _numFilas; i++)
		for (int j = 0; j < _numCols; j++)
			if (conts[i][j] != NULL)
				if (conts[i][j]->IsSelectable())
					EstablecerEnlaces(i, j);
}

Boton* MenuLayout::GetContByID(std::string id)
{
	for (int i = 0; i < _numFilas; i++)
		for (int j = 0; j < _numCols; j++)
			if (conts[i][j] != NULL)
				if (conts[i][j]->GetID() == id)
					return (Boton*)conts[i][j];
	return NULL;
}

Boton* MenuLayout::GetHighlighted()
{
	for (int i = 0; i < _numFilas; i++)
		for (int j = 0; j < _numCols; j++)
			if (conts[i][j] != NULL)
				if (conts[i][j]->EsHighlight())
					return (Boton*)conts[i][j]; 
	return NULL;
}

void MenuLayout::AsignarPadre(MenuLayout* menu)
{	
	supermenu = menu;   
}

void MenuLayout::AsignarSubMenu(MenuLayout* menu)
{	
	menu->AsignarPadre(this); 
	submenus[_numSubmenus] = menu; 
	_numSubmenus += 1;   
}

MenuLayout* MenuLayout::GetSubMenu(std::string id)
{
	if (_numSubmenus)
		for (int i = 0; i < _numSubmenus; i++)
			if (submenus[i]->GetID() == id)
				return submenus[i];
	return NULL;
}
	
void MenuLayout::EstablecerEnlaces(int x, int y)
{
	conts[x][y]->ToDir(DIR_ARR, conts[x][y]->GetID());
	conts[x][y]->ToDir(DIR_ABA, conts[x][y]->GetID());
	conts[x][y]->ToDir(DIR_IZQ, conts[x][y]->GetID());
	conts[x][y]->ToDir(DIR_DER, conts[x][y]->GetID());
	// Superiores...
	if (x > 0)
		for (int i = x-1; i > -1; i--)
			if (conts[i][y]!= NULL)
				if (conts[i][y]->IsSelectable() & conts[i][y]->EsVisible())
				{
					conts[x][y]->ToDir(DIR_ARR, conts[i][y]->GetID());
					break;
				}
	// Inferiores
	if (x < _numFilas -1)
		for (int i = x+1; i < _numFilas; i++)
			if (conts[i][y]!= NULL)
				if (conts[i][y]->IsSelectable() & conts[i][y]->EsVisible())
				{
					conts[x][y]->ToDir(DIR_ABA, conts[i][y]->GetID());
					break;
				}

	
	// Izquierda
	if (y > 0)
		for (int j = y-1; j > -1; j -- )
			if (conts[x][j]!= NULL)
				if (conts[x][j]->IsSelectable() & conts[x][j]->EsVisible())
				{
					conts[x][y]->ToDir(DIR_IZQ, conts[x][j]->GetID());
					break;
				}
	// Derecha
	if (y < _numCols - 1)
		for (int j = y+1; j < _numCols; j++)
			if (conts[x][j]!= NULL)
				if (conts[x][j]->IsSelectable() & conts[x][j]->EsVisible())
				{
					conts[x][y]->ToDir(DIR_DER, conts[x][j]->GetID());
					break;
				}
}

void MenuLayout::SeleccionarDefecto()
{   
	int found = 0;
	for (int i = 0; i < _numFilas; i++)
		for (int j = 0; j < _numCols; j++)
		{
			if (conts[i][j]!= NULL)
				conts[i][j]->SetHighlight(0);

		}
	
	for (int i = 0; i < _numFilas; i++)
	{
		for (int j = 0; j < _numCols; j++)
			if (conts[i][j]!= NULL)
				if (conts[i][j]->IsSelectable() & conts[i][j]->EsVisible())
				{
					found = 1;
					conts[i][j]->SetHighlight(1);
					break;
				}
		if (found)
			break;
	}
}

Elemento* MenuLayout::GetElemByID(std::string id)
{
	int found = 0;
	
	for (int i = 0; i < _numFilas; i++)
	{
		for (int j = 0; j < _numCols; j++)
			if (conts[i][j] != NULL)
			{
				if (conts[i][j]->GetElemByID(id) != NULL)
					return conts[i][j]->GetElemByID(id);
			}
	}
}

void MenuLayout::Dibujar()
{

	if (supermenu != NULL)
		supermenu->Dibujar();

	ScreenLayout::SetOrtho();

	// "Overlay"
	glEnable(GL_BLEND);
	glColor4f(0.0, 0.0, 0.0, _transparency);
	glBegin(GL_QUADS);
		glVertex2i(0, 0);
		glVertex2i(0, _pantY);
		glVertex2i(_pantX, _pantY);
		glVertex2i(_pantX, 0);
	glEnd();
	glColor4f(0.0, 0.0, 0.0, 1.0);
	glDisable(GL_BLEND);
	
	// "Contenedores propios"
	for (int i = 0; i < _numFilas; i ++)
		for (int j = 0; j < _numCols; j++)
			if (conts[i][j] != NULL)
			    conts[i][j]->Dibujar();

	
	ScreenLayout::UnsetOrtho();
}